//
//  CarModel.m
//  carcareIOS
//
//  Created by ileo on 16/8/3.
//  Copyright © 2016年 chezheng. All rights reserved.
//

#import "CarModel.h"


@interface CarModel ()

@property (nonatomic, copy) NSDictionary *carinfo;
@property (nonatomic, copy) NSArray *cities;

@end

@implementation CarModel

-(void)setCarinfo:(NSDictionary *)carinfo{
    _carinfo = [carinfo copy];
    self.carinfoModel = [[CarInfoModel alloc] initWithDic:carinfo];
}

-(void)setCities:(NSArray *)cities{
    _cities = cities;
    NSMutableArray *tmp = [NSMutableArray arrayWithCapacity:cities.count];
    [cities enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        VioCityModel *model = [[VioCityModel alloc] initWithDic:obj];
        [tmp addObject:model];
    }];
    self.cityModels = [tmp copy];
}

-(void)setCityModels:(NSArray *)cityModels{
    _cityModels = cityModels;
    NSMutableString *show = [NSMutableString stringWithCapacity:3];
    NSMutableString *cIDs = [NSMutableString stringWithCapacity:3];
    __block BOOL needIDNum = NO;
    [cityModels enumerateObjectsUsingBlock:^(VioCityModel *  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (obj.needIdNum) needIDNum = YES;
        if (idx != 0) {
            [cIDs appendString:@","];
            if (idx < 2) {
                [show appendString:@","];
            }
        }
        [cIDs appendString:[NSString stringWithFormat:@"%zd",obj.cid]];
        if (idx < 2) {
            [show appendString:obj.name];
        }
    }];
    if (cityModels.count > 2) {
        [show appendString:@"等"];
    }
    self.needIDNum = needIDNum;
    self.showCitys = [show copy];
    self.carinfoModel.driveCity = cIDs;

}

-(BOOL)isVioInfoComplete{
    if (self.cityModels.count > 0 && [self.carinfoModel.carno isNotNil] && [self.carinfoModel.ecode isNotNil] && [self.carinfoModel.vcode isNotNil]) {
        return !self.needIDNum || (self.needIDNum && [self.carinfoModel.idnum isNotNil]);
    }else{
        return NO;
    }
}

@end


